# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

include $(NACL_SDK_ROOT)/tools/common.mk

TARGET = kernel
LIBS = freetype bz2 ssl crypto z png readline ncurses
NACL_LDFLAGS += -lpython2.7 $(NACL_CLI_MAIN_LIB_CPP)
PNACL_LDFLAGS += -lpython2.7 $(NACL_CLI_MAIN_LIB_CPP)
ifeq ($(TOOLCHAIN),glibc)
LIBS += util dl
NACL_LDFLAGS += -Wl,-export-dynamic
else
LIBS += glibc-compat
endif
SOURCES = kernel.cc
INC_PATHS = $(NACLPORTS_INCLUDE)/python2.7
INSTALL_DIR = $(NACL_PACKAGES_PUBLISH)/ipython/$(TOOLCHAIN)

ifeq ($(TOOLCHAIN),pnacl)
EXEEXT=.pexe
else
EXEEXT=.nexe
endif

ifneq ($(TOOLCHAIN),pnacl)
# We want the nmf to contain all the .nexe we have previously
# built as well as the one currently being built, so we modify
# the list of executables that we pass to create_nmf.
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_64$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_32$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_arm$(EXEEXT))
endif

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

ifeq ($(TOOLCHAIN),glibc)
EXTRA_ASSETS = lib/libz.so.1 lib/libreadline.so lib/libncurses.so.5
endif

install:
	mkdir -p $(INSTALL_DIR)
	tar -h -c -C ${NACL_PREFIX} -f $(INSTALL_DIR)/pydata_$(NACL_ARCH).tar lib/python2.7 ${EXTRA_ASSETS}
	cp $(OUTDIR)/$(TARGET)*$(EXEEXT) $(INSTALL_DIR)
	rm -f $(INSTALL_DIR)/*_unstripped*$(EXEEXT)
	cp $(OUTDIR)/$(TARGET).nmf $(INSTALL_DIR)
	cp kernel.py $(INSTALL_DIR)
ifeq ($(TOOLCHAIN),glibc)
	cp -r $(OUTDIR)/lib* $(INSTALL_DIR)
endif
